package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.AppletServer;
import com.ruoyi.fly.domain.ServerOrder;
import com.ruoyi.fly.domain.Store;
import com.ruoyi.fly.domain.Vehicles;
import com.ruoyi.fly.domain.vo.ServerOrderVo;
import com.ruoyi.fly.mapper.AppletServerMapper;
import com.ruoyi.fly.mapper.ServerOrderMapper;
import com.ruoyi.fly.mapper.StoreMapper;
import com.ruoyi.fly.mapper.VehiclesMapper;
import com.ruoyi.fly.service.ServerOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ServerOrderServiceImpl implements ServerOrderService {

    @Autowired
    private ServerOrderMapper serverOrderMapper;

    @Autowired
    private VehiclesMapper vehiclesMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private AppletServerMapper appletServerMapper;

    /**
     * 根据id查询服务订单
     * @param id
     * @return
     */
    @Override
    public ServerOrderVo selectAppletServerOrderById(String id) {
        ServerOrderVo serverOrderVo = new ServerOrderVo();
        ServerOrder serverOrder = serverOrderMapper.selectAppletServerOrderById(id);
        Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(serverOrder.getVehiclesId());
        Store store = storeMapper.selectStoreById(serverOrder.getStoreId());
        AppletServer appletServer = appletServerMapper.selectAppletServerById(serverOrder.getServerId());
        BeanUtils.copyProperties(serverOrder, serverOrderVo);
        serverOrderVo.setLicensePlate(vehicles.getLicensePlate());
        serverOrderVo.setStoreName(store.getStoreName());
        serverOrderVo.setTitle(appletServer.getTitle());
        return serverOrderVo;
    }

    /**
     * 查询服务订单列表
     * @param serverOrder
     * @return
     */
    @Override
    public List<ServerOrderVo> selectAppletServerOrderList(ServerOrder serverOrder) {
        List<ServerOrder> serverOrders = serverOrderMapper.selectAppletServerOrderList(serverOrder);
        List<ServerOrderVo> serverOrderVos = new ArrayList<>();
        ServerOrderVo serverOrderVo = new ServerOrderVo();
        serverOrders.stream().forEach(serverOrder1 -> {
            Vehicles vehicles = vehiclesMapper.selectPcVehiclesById(serverOrder1.getVehiclesId());
            Store store = storeMapper.selectStoreById(serverOrder1.getStoreId());
            AppletServer appletServer = appletServerMapper.selectAppletServerById(serverOrder1.getServerId());
            BeanUtils.copyProperties(serverOrder1, serverOrderVos);
            serverOrderVo.setLicensePlate(vehicles.getLicensePlate());
            serverOrderVo.setStoreName(store.getStoreName());
            serverOrderVo.setTitle(appletServer.getTitle());
            serverOrderVos.add(serverOrderVo);
        });
        return serverOrderVos;
    }

    /**
     * 新增服务订单
     * @param serverOrder
     * @return
     */
    @Override
    public R insertAppletServerOrder(ServerOrder serverOrder) {
        serverOrder.setCreateTime(DateUtils.getNowDate());
        serverOrder.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return serverOrderMapper.insertAppletServerOrder(serverOrder) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }

    /**
     * 修改服务订单
     * @param serverOrder
     * @return
     */
    @Override
    public R updateAppletServerOrder(ServerOrder serverOrder) {
        serverOrder.setUpdateTime(DateUtils.getNowDate());
        serverOrder.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return serverOrderMapper.updateAppletServerOrder(serverOrder) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 删除服务订单
     * @param id
     * @return
     */
    @Override
    public R deleteAppletServerOrderById(String id) {
        return serverOrderMapper.deleteAppletServerOrderById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteAppletServerOrderByIds(String[] id) {
        return serverOrderMapper.deleteAppletServerOrderByIds(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
